"""
@author: Kenneth Tse  xie.kenneth@gmail.com
"""
import socketserver
import serverfactory
from time import sleep
import threading
import socket
from state import StateProcess
import client

class ClientHandler(socketserver.BaseRequestHandler):
    def handle(self):
        """
        print(self.request)
        sleep(1)
        print(self.server)
        """
        stateproc = StateProcess(self)
        stateproc.process()
        self.request.close()

"""
def client(ip, port, message):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    sock.send(message)
    #response = sock.recv(1024)
    #print("Received: %s" % response)
    sock.close()
"""

if __name__ == "__main__":
    host = "localhost"
    port = 20008
    server = serverfactory.createServer(host, port, ClientHandler)
    server_thread = threading.Thread(target=server.serve_forever) 
    server_thread.setDaemon(True)
    server_thread.start()
    
    """
    client(host, port, b"hhhh")
    client(host, port, b"aaaa")
    """
    #client.echo(host, port, b"VER:001\nhaha")
    client.echo(host, port, b"ECHO:test\n")
    
    sleep(3000)
    server.shutdown()
    print("Hello shit!")
    
    
    
